package com.dede.monochromatism.base

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import com.dede.applib.util.ExitUtil
import com.dede.applib.util.LogExtends
import com.dede.applib.util.d
import org.jetbrains.anko.defaultSharedPreferences

/**
 * @author hsh
 * @date 2017/8/17 14:00.
 * @doc
 */
open class BaseActivity : AppCompatActivity(), LogExtends {

    open val sp by lazy { defaultSharedPreferences }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        ExitUtil.addActivity(this)
        d("onCreate")
    }

    override fun onDestroy() {
        super.onDestroy()
        ExitUtil.removeActivity(this)
        d("onDestroy")
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (showBack && item.itemId == android.R.id.home) {
            onBackPressed()
            return true
        }
        return super.onOptionsItemSelected(item)
    }

    private var showBack = false

    protected fun showBackButton(show: Boolean) {
        this.showBack = show
        supportActionBar?.setDisplayHomeAsUpEnabled(showBack)
    }

}